/*
 * 
 */

package multiplieralgorithm;

/**
 *
 * @author Kylan Robinson (krobinso@eecs.wsu.edu)
 */
public class OpArray{
    
    int m;                  //bits per array entry
    int value;              //the overall value of the array
    int[] array;            //array representation of value
    
    //default constructor
    public OpArray() {
        m = 0;              //default number of bits is invalid
        value = 0;          //default value is 0
        array = new int[0]; //default array is empty
    }
    
    //regular constructor
    public OpArray(int new_val, int new_m, int dim) {
        value = new_val;        //must have an m-bit binary representation
        m = new_m;
        array = new int[dim];   //there will be dim entries in the array
        
        split_value();
    }
    
    //split the value into portions
    public void split_value() {
        int i;
        int mask = create_mask();
        
        //for each entry of the array
        for(i = 0; i < array.length; i++) {
            //shift in m bits, from LSB to MSB
            array[i] = (value >> i*m) & mask;
        }
    }
    
    public int toInt() {
        value = 0;
        for(int i = 0; i < array.length; i++) {
            value = value | (array[i] << i*m);
        }
        
        return value;
    }
    
    public void print_values() {
        int i;
        
        //for each entry of the array
        for(i = 0; i < array.length; i++) {
            //shift in m bits, from LSB to MSB
            System.out.print(array[i]);
        }
        System.out.print('\n');
    }
    
    public int create_mask() {
        return (int)Math.pow(2, m) - 1;
    }
    
}
